package com.sec.android.app.myfiles.facade.cmd;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.AbsMyFilesActivity;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.PrivateModeMgr;
import com.sec.android.app.myfiles.feature.PrivateModeMgr.PrivateStateListener;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.operation.FileOperationFragment.PostOperation;
import com.sec.android.app.myfiles.operation.FileOperator;
import com.sec.android.app.myfiles.operation.FileOperator.Operation;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.util.ArrayList;

public class MoveToPrivateCmd extends SimpleCommand implements PrivateStateListener, PostOperation {

    private PrivateModeMgr mPrivateModeMgr;

    private int mProcessId;
    private Context mContext;
    private AbsMyFilesFragment mFragment;
    private ArrayList<FileRecord> mSrcRecordList;
    private ArrayList<FileRecord> mNonPrivateRecordList;
    private FileRecord mDst;
    private String mPrivateRoot;

    @SuppressWarnings("unchecked")
    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        mProcessId = (Integer) params[0];
        mContext = (Context) params[1];
        mFragment = (AbsMyFilesFragment) params[2];
        mSrcRecordList = (ArrayList<FileRecord>) params[3];

        mPrivateModeMgr = PrivateModeMgr.getInstance(mContext);

        mPrivateRoot = mPrivateModeMgr.getRootDir();

        mDst = FileRecord.createFileRecord(StorageType.Local, mPrivateRoot);

        if (mPrivateModeMgr.isPrivateDirMounted() == false) {
            if (mFragment.getActivity() instanceof AbsMyFilesActivity) {
                if (!((AbsMyFilesActivity) mFragment.getActivity()).isPinWindow()) {
                    mPrivateModeMgr.createMgr(this);
                } else {
                    //Starting Random activity to show unpin window message
                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            }
        } else {
            doOperate();
        }

    }

    public void doOperate() {
        mNonPrivateRecordList = new ArrayList<>();

        for (FileRecord fileRecord : mSrcRecordList) {
            if (fileRecord != null && !fileRecord.getPath().startsWith(mPrivateRoot)) {
                mNonPrivateRecordList.add(fileRecord);
            }
        }

        MyFilesFacade.copymoveRecord(mProcessId, mContext, Operation.MOVE_TO_PRIVATE, CopyMoveCmd.OperationProgress.DO_OPERATE, mFragment, mNonPrivateRecordList,
                mDst, this);
    }

    @Override
    public void onMounted(int extInfo) {
        Log.d(this, "onMounted : " + extInfo);
        doOperate();
    }

    @Override
    public void onPrepare(int extInfo) {
        Log.d(this, "OnPrepared : " + extInfo);
    }

    @Override
    public void onCancel(int extInfo) {
        Log.d(this, "onCancel : " + extInfo);
        FileOperator.cancelIfExists();
    }

    @Override
    public void onUnmounted(int extInfo) {
        Log.d(this, "onUnmounted : " + extInfo);
    }

    @Override
    public void onResult(Operation opType, boolean bRet, Bundle extras) {
        Log.d(this, "result  : " + bRet);
        mPrivateModeMgr.clearMgr(bRet);

        resultCheck(bRet, extras);
    }

    private void resultCheck(boolean result, Bundle extras) {
        if (result) {
            int normalItemCount = mNonPrivateRecordList.size();
            int privateItemCount = mSrcRecordList.size() - normalItemCount;
            if (extras != null) {
                normalItemCount -= extras.getInt(FileOperator.SKIPPED_FILES, 0);
            }
            if (normalItemCount > 0) {
                if (privateItemCount > 0) {
                    Toast.makeText(mContext, makeMessage(privateItemCount, normalItemCount), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, mContext.getResources().getQuantityString(R.plurals.items_moved_to_private, normalItemCount, normalItemCount), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private String makeMessage(int privateItemCount, int normalItemCount) {
        String msg = "";

        if (normalItemCount > 1) {
            msg = mContext.getResources().getQuantityString(R.plurals.n_itmes_moved_to_private, privateItemCount, normalItemCount);
        } else {
            msg = mContext.getResources().getQuantityString(R.plurals.one_item_moved_to_private, privateItemCount);
        }

        return msg;
    }
}
